<?php
/**
 * text
 *
 * script
 *
 * @version:	0.1a
 * @package:	global
 * @author:	alex_ez
 * @created:	27.01.2009 01:52:13
 *
 * @copyright:	(c) 2009 alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved.
 */


class pages_admin_controller extends admin_controller
{
	static public function get_menu_items( )
	{
//		return array(
//			'routes' => array( 'text' => __('Routes'), 'section' => 'content' )
//			);
	}
	
	public function on_start( )
	{
		$this ->visitor = visitor::instance( );
		$this ->visitor ->need_auth( 'admin' );
		$this ->frame = 'admin/!index.tpl';
		$this ->pages = new pages( );
	}
	
	private $field_sets = array(
'admin/pages/add' =>
"{'head1':		{type:'header',	label:'Данные об урле'},
'url':			{type:'string',	label:'Путь','value':'/',required:true},
'action':		{type:'list',	label:'Метод',values:'actions',required:true},
'state':		{type:'list',	label:'Состояние',value:'1',values:'states',required:true},
'priority':		{type:'number',	label:'Порядок',value:10,required:true},
'headoptions':		{type:'header',	label:'Опции'},
'options[frame]':	{type:'list',	label:'Общий шаблон',values:'frames'},
'options[template]':	{type:'string',	label:'Шаблон центра'}}",
);

	public function get_field_set( $url = null, $decode = false )
	{
		$iurl = is_null( $url ) ? $this ->getInnerUrl( ) : $url;
		$r = isset( $this ->field_sets[ $iurl ] ) ? $this ->field_sets[ $iurl ] : ''; //json::decode( $fields );
		
		$replacements = array(
			'now' => date('d.m.Y'),
			'yesterday' => date('d.m.Y',time()-86400),
			'tomorrow' => date('d.m.Y',time()+86400),
			'user.login' => visitor::instance() ->user['login'],
			);
		
		// replacing
		$r = str_replace(
			array_map( create_function('$v','return "{{$v}}";'), array_keys( $replacements ) ),
			array_values( $replacements ),
			$r );
		
		return $decode ? json::decode( $r ) : $r;
	}
	
	
	
	public function page_index( $p, $o )
	{
		$r = array( );
		$r['list'] = $this ->pages ->by( array( ), 'priority,id' );
		$r['title'] = 'Урлы';
		
		return $r;
	}
	
	public function page_add( $p, $o )
	{
		$this ->pageTitle = 'xxx';
		
		$form = new Form( $this, 'admin/pages/add' );
		$form ->set_option( 'name', 'addpage' );
		$form ->set_option( 'action', '/ajax/admin/pages/add/' );
		
		$actions = controller::getPublicPages( );
		$actions = _a::flat( $actions, 'action' );
		bc::log( $actions, '__pages' );
		$form ->set_list( 'actions', $actions, '{action} [{class}]' );
		$form ->set_list( 'states', array( 'off' => 'off', 'dev' => 'dev', 'on' => 'on' ) );
		$form ->set_list( 'frames', array( 'default' => '', '!main.tpl' => '!main.tpl' ) );
		//$form ->set_list( 'currencies', $accs->getCurrencies( ), '{name_ru} [{symbol}]' );
		//$form ->set_list( 'accounts', $accs->getAccounts( ), strtoupper( $user_login ).'.{title}.{currency_ticker}' );
		
		$r['fields'] = $form ->get_field_set( );
		$r['options'] = $form ->get_options( );
		$r['template'] = 'admin/__add.tpl';
		
		bc::log( $r ); //"{'action':'/ajax/info/cashless/add/','method':'POST','name':'addoffer'}";
		
		return $r;
	}
	
	public function ajax_add( $z )
	{
		$form = new Form( $this, 'admin/pages/add' );
		
		// fetch post data
		try { $p = $form ->get_post_data( ); }
		// if data is bogus return error
		catch( form_exception $e ) { return array( 'error' => $e ->getMessage() ); }
		
		if( $p['options']['frame'] == 'default' ) $p['options']['frame'] = '';
		$p['options'] = json::encode( array_filter( $p['options'] ) );
		
		$pages = new pages( );
		$id = $pages ->add( $p );
		if( $id<=0 ) return array( 'error' => 'Путь не добавлен. Внутренняя ошибка.' );
		
		return array( 'message' => 'Путь добавлен. id: '.$id, 'location' => '/admin/pages/' );
	}
	
	//new textModel( );
	
	public function page_edit( $p, $o )
	{
		$this ->pageTitle = 'edit';
		
		$id = (int)(Request::instance( ) ->g_id);
		
		$p = $this ->pages ->get( $id );
		if( !$p ) Response::instance( ) ->notfound( );
		
		$form = new Form( $this, 'admin/pages/add' );
		$form ->set_option( 'name', 'editpage' );
		$form ->set_option( 'action', '/ajax/admin/pages/edit/?id='.$id );
		$form ->set_data( $p );
		
		$pages = controller::getPublicPages( );
		$pages = _a::flat( $pages, 'action' );
		bc::log( $pages, '__pages' );
		$form ->set_list( 'actions', $pages, '{action} [{class}]' );
		$form ->set_list( 'states', array( 'off' => 'off', 'dev' => 'dev', 'on' => 'on' ) );
		$form ->set_list( 'frames', array( 'default' => '', '!main.tpl' => '!main.tpl' ) );
		//$form ->set_list( 'currencies', $accs->getCurrencies( ), '{name_ru} [{symbol}]' );
		//$form ->set_list( 'accounts', $accs->getAccounts( ), strtoupper( $user_login ).'.{title}.{currency_ticker}' );
		
		$r['fields'] = $form ->get_field_set( );
		$r['options'] = $form ->get_options( );
		$r['template'] = 'admin/__add.tpl';
		
		bc::log( $r ); //"{'action':'/ajax/info/cashless/add/','method':'POST','name':'addoffer'}";
		
		return $r;
	}
}

?>